
/**
 * 根据关键字搜索地名与位置
 */
function searchLocation(key,cb){
  getApp().data. qqmapsdk.search({
      keyword: key,
      success: function (res) {
        return typeof cb == "function" && cb(res)
      },
      fail: function (res) {
        return typeof cb == "function" && cb(res)
      },
      complete: function (res) {
      }
    });
}

/**
 * 根据经纬度获取详细地址信息
 */
function getAddress (latitude, longitude,cb) {
  getApp().data.qqmapsdk.reverseGeocoder({
      location: {
        latitude: latitude,
        longitude: longitude
      },
      success: function (res) {
       // console.log(JSON.stringify(res));
        let province = res.result.ad_info.province
        let city = res.result.ad_info.city
        return typeof cb == "function" && cb(res)
      },
      fail: function (res) {
        return typeof cb == "function" && cb(false)
      },
      complete: function (res) {
      }
    });
}

/**
 * 获取地理位置的经纬度
 */

function getLocationDate(cb){
  wx.getLocation({
    type: 'wgs84',
    success(res) {
      const latitude = res.latitude
      const longitude = res.longitude
      const speed = res.speed
      const accuracy = res.accuracy
      getAddress(latitude, longitude, function (res) {
        return typeof cb == "function" && cb(res)
      })
    }
  })
}

/**
 * 将方法暴露出去供其他类进行调用
 */
module.exports.searchLocation = searchLocation
module.exports.getAddress = getAddress
module.exports.getLocationDate = getLocationDate